package cn.shoukaiseki.sjf4j.fastjson;

import cn.shoukaiseki.sjf4j.JsonException;
import cn.shoukaiseki.sjf4j.Sjf4j;
import cn.shoukaiseki.sjf4j.node.TreeNode;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONFactory;

/**
 * fastjson2 的 sjf4j 实现类
 * <br>
 * cn.shoukaiseki.sjf4j.impl.FastJson2Impl
 * <br>
 * @author shoukaiseki
 * 主页 https://gitee.com/shoukaiseki/<br>
 * E-mail jiang28555@qq.com<br>
 * @date 2024-05-22 17:44:33<br>
 */
public class Fastjson2Impl implements Sjf4j {

    public Fastjson2Impl(){
        JSONFactory.setUseJacksonAnnotation(true);
    }

    @Override
    public String toJsonString(Object obj) {
        try{
            return JSON.toJSONString(obj);
        }catch (Throwable t){
            throw new JsonException(t);
        }
    }

    @Override
    public <T> T jsonToObj(String json, Class<T> clazz) {
        try{
            return JSON.parseObject(json,clazz);
        }catch (Throwable t){
           throw new JsonException(t);
        }
    }

    @Override
    public TreeNode jsonToObj(String json) {
        Object obj = JSON.parse(json);
        return FastjsonTreeNode.convert(obj);
    }

    @Override
    public TreeNode jsonToArrayNode(String json) {
        return null;
    }

    @Override
    public TreeNode jsonToObjectNode(String json) {
        return null;
    }
}
